package com.example.newsapplication.fragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.newsapplication.R;
import com.example.newsapplication.activity.LoginActivity;
import com.example.newsapplication.activity.MyCollectActivity;

import org.jetbrains.annotations.Nullable;

import butterknife.BindView;
import butterknife.OnClick;
import skin.support.SkinCompatManager;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyFragment extends BaseFragment {


    private RelativeLayout rl_collect;
    private RelativeLayout rl_skin;
    private RelativeLayout rl_logout;

    public MyFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        Log.d("MyFragment", "initLayout called");
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        rl_collect = mRootView.findViewById(R.id.rl_collect);
        rl_skin = mRootView.findViewById(R.id.rl_skin);
        rl_logout = mRootView.findViewById(R.id.rl_logout);
    }

    @Override
    protected void initData() {
        Log.d("MyFragment", "initData called");
        rl_collect.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                navigateTo(MyCollectActivity.class);
            }
        });
        rl_skin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String skin = findByKey("skin");
                if (skin.equals("night")) {
                    // 恢复应用默认皮肤
                    SkinCompatManager.getInstance().restoreDefaultTheme();
                    insertVal("skin", "default");
                } else {
                    SkinCompatManager.getInstance().loadSkin("night", SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); // 后缀加载
                    insertVal("skin", "night");
                }
            }
        });
        rl_logout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                removeByKey("token");
                navigateToWithFlag(LoginActivity.class,
                        Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        });
    }
}